Page({

  /**
   * 页面的初始数据
   */
  data: {

    //轮播参数设置
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    carousel:[],
    
    goods: [],

  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this

    //请求首页轮播数据
    wx.request({
      url: 'https://api.it120.cc/tz/banner/list',
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({carousel: res.data.data });
        }
      }
    })

  //请求产品类型数据
    wx.request({
      url: 'https://api.it120.cc/tz/shop/goods/category/all',
      success: function (res) {
        var categories = [{ id: 0, name: "全部" }];
        if (res.data.code == 0) {
          for (var i = 0; i < res.data.data.length; i++) {
            categories.push(res.data.data[i]);
          }
        }
        that.setData({
          categories: categories,
            activeCategoryId: 0
        });
        that.getGoodsList(0);
      }
    })
  },

  //切换轮播图片事件
  swiperchange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },

  //点击轮播图片事件，查看产品详情
  toDetailsTap: function (e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/goodsdetails/goodsdetails?id=" + e.currentTarget.dataset.id
      })
    }
  },

  //点击产品类型选项卡
  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getGoodsList(this.data.activeCategoryId);
  },


  //获取产品信息
    getGoodsList: function (categoryId) {
    var that = this;
    if (categoryId == 0) {
      categoryId = "";
    }
    wx.request({
      url: 'https://api.it120.cc/tz/shop/goods/list',
      data: {
        categoryId: categoryId
      },
      success: function(res) {

        //选择产品类别没有商品
        if (res.data.code != 0 || res.data.data.length == 0) {
          that.setData({
            loadingMoreHidden:false,
          });
          return;
        }

       //选择产品类别下的所有商品
        var goods = [];
        for(var i=0;i<res.data.data.length;i++){
          goods.push(res.data.data[i]);
        }

        that.setData({
          loadingMoreHidden: true,
          goods:goods
        });

      }
    })
  },

})